package cn.iocoder.yudao.module.pplan.service.recipeaddrecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.recipeaddrecord.vo.RecipeAddRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipeaddrecord.vo.RecipeAddRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipeaddrecord.RecipeAddRecordDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.recipeaddrecord.RecipeAddRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.RECIPE_ADD_RECORD_NOT_EXISTS;

/**
 * 生产计划-加餐记录 Service 实现类
 *
 * @author yisla
 */
@Service
@Validated
public class RecipeAddRecordServiceImpl implements RecipeAddRecordService {

    @Resource
    private RecipeAddRecordMapper recipeAddRecordMapper;

    @Override
    public Long createRecipeAddRecord(RecipeAddRecordSaveReqVO createReqVO) {
        // 插入
        RecipeAddRecordDO recipeAddRecord = BeanUtils.toBean(createReqVO, RecipeAddRecordDO.class);
        recipeAddRecord.setRecordDate(LocalDate.parse(createReqVO.getRecordDate()));
        recipeAddRecordMapper.insert(recipeAddRecord);
        // 返回
        return recipeAddRecord.getId();
    }

    @Override
    public void updateRecipeAddRecord(RecipeAddRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRecipeAddRecordExists(updateReqVO.getId());
        // 更新
        RecipeAddRecordDO updateObj = BeanUtils.toBean(updateReqVO, RecipeAddRecordDO.class);
        recipeAddRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecipeAddRecord(Long id) {
        // 校验存在
        validateRecipeAddRecordExists(id);
        // 删除
        recipeAddRecordMapper.deleteById(id);
    }

    @Override
        public void deleteRecipeAddRecordListByIds(List<Long> ids) {
        // 校验存在
        validateRecipeAddRecordExists(ids);
        // 删除
        recipeAddRecordMapper.deleteByIds(ids);
        }

    private void validateRecipeAddRecordExists(List<Long> ids) {
        List<RecipeAddRecordDO> list = recipeAddRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECIPE_ADD_RECORD_NOT_EXISTS);
        }
    }

    private void validateRecipeAddRecordExists(Long id) {
        if (recipeAddRecordMapper.selectById(id) == null) {
            throw exception(RECIPE_ADD_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RecipeAddRecordDO getRecipeAddRecord(Long id) {
        return recipeAddRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RecipeAddRecordDO> getRecipeAddRecordPage(RecipeAddRecordPageReqVO pageReqVO) {
        return recipeAddRecordMapper.selectPage(pageReqVO);
    }

}